package com.zdy.showgoods;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import com.alibaba.fastjson.JSONObject;
import com.example.networklibrary.BaseObserver;
import com.example.networklibrary.MD5;
import com.example.networklibrary.RxHttp;
import com.example.networklibrary.SharedPreferencesUtils;
import com.example.networklibrary.ToastUtils;
import com.example.networklibrary.URLInterface;
import com.example.networklibrary.config.KeyInterface;
import com.example.networklibrary.entity.IMEI;
import com.example.networklibrary.entity.RequestEntity;
import com.example.networklibrary.entity.Header;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zdy.modellibrary.base.MYApplication;
import com.zdy.service.RFIDReadService;
import com.zdy.utils.AppUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Key;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ActivityLauncher extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        ToastUtils.init(getApplication());
//        SharedPreferencesUtils.setString(this,KeyInterface.AppURL,"http://local.zhiyis.com:6001/rpc.api");
        if (TextUtils.isEmpty(SharedPreferencesUtils.getString(this, KeyInterface.AppURL))) {
            requestPermissions();
        } else {
            toMain();
        }
    }

    private void requestPermissions() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission
                .request(Manifest.permission.READ_PHONE_STATE)
                .subscribe(granted -> {
                    if (granted) { // Always true pre-M
                        getAddress();
                    } else {
                        //未授权
                        Toast.makeText(ActivityLauncher.this, "请授权", Toast.LENGTH_SHORT).show();
                    }
                });
    }


    private void getAddress() {
        RequestEntity requestEntity = new RequestEntity();
        IMEI imei = new IMEI();
//        imei.setDevice_imei(AppUtil.getMac());
        imei.setDevice_imei("bbde5040c6bab57c");
        requestEntity.setBody(imei);
        Header requestHeader = new Header();
        requestHeader.setTra_code(KeyInterface.TRAD_NO_GET_ADD);
        requestHeader.setRequest_seq(KeyInterface.REQUEST_SEQ);
        requestHeader.setSign("");
        requestHeader.setSys_id(KeyInterface.SYS_ID);
        requestEntity.setHeader(requestHeader);
        try {
            String signString = JSONObject.toJSONString(requestEntity);
            String sign = MD5.getSign(signString);
            requestHeader.setSign(sign);
            requestEntity.setHeader(requestHeader);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String json = "";
        try {
            json = URLEncoder.encode(JSONObject.toJSONString(requestEntity), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RxHttp.getInstance()
                .getSyncServer()
                .parseJsonObject(json)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject result) {
                        try {
                            String appURL = result.getString("api_url");
                            SharedPreferencesUtils.setString(ActivityLauncher.this, KeyInterface.AppURL, appURL);
                            toMain();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        Log.e("onFailure", errorMsg);
                    }
                });
    }

    private void toMain() {
        Intent intent = new Intent(this, ActivityLogin.class);
        startActivity(intent);
        finish();
    }


}
